package ar.com.ctrln.view.dashboard.administracion.facturacion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;

import ar.com.ctrln.control.administracion.facturacion.ConsultarFactura;
import ar.com.ctrln.persistence.model.factura.DetalleFactura;
import ar.com.ctrln.persistence.model.factura.Factura;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ConsultarFacturas extends AbstractMinimizarVentana {

	private static final long serialVersionUID = 1285016165233381585L;

	@WireVariable
	private ConsultarFactura consultarFactura;

	@Wire
	private Listbox lbFacturas;

	@Wire
	private Div btnImprimir;

	@Wire
	private Listbox lbDetalleFactura;

	private Connection con;

	private ListModelList<Factura> modeloF = new ListModelList<Factura>();
	private ListModelList<DetalleFactura> modeloD = new ListModelList<DetalleFactura>();

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		modeloF.addAll(consultarFactura.listarFacturas());
		lbFacturas.setModel(modeloF);

	}

	@Listen("onSelect = #lbFacturas")
	public void mostrarDetalles() {
		if (null != lbFacturas.getSelectedItem()) {
			Factura factura = lbFacturas.getSelectedItem().getValue();
			modeloD.clear();
			modeloD.addAll(consultarFactura.listarDetalles(factura.getId()));
			lbDetalleFactura.setModel(modeloD);

		} else {
			Clients.showNotification("Seleccione una Factura", lbFacturas);
		}

	}

	@Listen("onClick = #btnImprimir")
	public void imprimirFactura() throws SQLException {

		if (lbFacturas.getSelectedItem() != null) {

			Factura factura = lbFacturas.getSelectedItem().getValue();

			SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
			try {
				Class.forName("com.mysql.jdbc.Driver");
				con = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/ctrln", "root", "root");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

			String path = this.getPage().getDesktop().getWebApp()
					.getRealPath("/resources/reportes/factura.jrxml");
			String camino = path.replaceAll("\\\\", "/");

			try {
				Map param = new HashMap();
				param.put("pIdFactura", (int) factura.getId());

				JasperReport jasperReport = JasperCompileManager
						.compileReport(camino);
				JasperPrint jasperPrint = JasperFillManager.fillReport(
						jasperReport, param, con);
				JasperViewer.viewReport(jasperPrint, false);
				con.close();
			} catch (JRException e) {
				e.printStackTrace();
			}

		} else {
			Clients.showNotification("Seleccione una Factura", lbFacturas);
		}
	}

}
